#include "log.h"

#include <stdarg.h>
#include <time.h>

FILE *log_file;
int log_verbose = 0;

static void log_err_args(char *tag, char *fmt, va_list args) {
    FILE *err_file = stderr;
    if (log_file != stdout)
        err_file = log_file;
    fprintf(err_file, "(%lu) %s: ", (unsigned long)time(NULL), tag);
    vfprintf(err_file, fmt, args);
}

void log_err(char *tag, char *fmt, ...) {
    va_list args;
    va_start (args, fmt);
    log_err_args(tag, fmt, args);
    va_end (args);
}

static void log_out_args(char *tag, char *fmt, va_list args) {
    fprintf(log_file, "(%lu) %s: ", (unsigned long)time(NULL), tag);
    vfprintf(log_file, fmt, args);
}

void log_out(char *tag, char *fmt, ...) {
    va_list args;
    va_start (args, fmt);
    log_out_args(tag, fmt, args);
    va_end (args);
}

void log_out_v(char *tag, char *fmt, ...) {
    if (!log_verbose)
        return;
    va_list args;
    va_start (args, fmt);
    log_out_args(tag, fmt, args);
    va_end (args);

}
